using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace Juego
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        KeyboardState keyboardState;
        SpriteFont Font1;

        Texture2D background;
        Texture2D player1Texture;
        Player player1 = new Player();

        Texture2D player2Texture;
        Player player2 = new Player();

        Texture2D BolaTexture;
        Bola bola = new Bola();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;

            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //player1 = new Player();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            background = Content.Load<Texture2D>("background_pong");
            player1Texture = Content.Load<Texture2D>("player1");
            BolaTexture = Content.Load<Texture2D>("ball");
            Font1 = Content.Load<SpriteFont>("SpriteFont1");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed){
                this.Exit();
            }

            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A))
            {
                this.Exit();
            }

            if( Keyboard.GetState().IsKeyDown(Keys.Escape) ){
                this.Exit();
            }

            if( Keyboard.GetState().IsKeyDown(Keys.W) ){
                if(player1.posicion > Player.MIN_POS)
                    player1.moveUp();
            }

            if( Keyboard.GetState().IsKeyDown(Keys.S) )
            {
                if (player1.posicion < (GraphicsDevice.Viewport.Height - (Player.MIN_POS*2 ) - player1Texture.Height/3) )
                player1.moveDown();
            }

            // TODO: Add your update logic here
            bola.mover();
            if (bola.posicion.X <= 0 || bola.posicion.X >= GraphicsDevice.Viewport.Width - BolaTexture.Width)
            {
                bola.velocidad.X *= -1;
            }
            else if (bola.posicion.Y <= 0 || bola.posicion.Y >= GraphicsDevice.Viewport.Height - BolaTexture.Height)
            {
                bola.velocidad.Y *= -1;
            }


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            // dibuja el fondo del juego
            spriteBatch.Draw(
                background, 
                new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), 
                Color.White
                );
            
            
            //dibuja a el primer jugador
            spriteBatch.Draw(
                player1Texture, 
                new Rectangle(10, player1.posicion, player1Texture.Width/3, player1Texture.Height/3),
                Color.White
                );

            //dibuja la bola
            spriteBatch.Draw(
                BolaTexture,
                bola.posicion,
                Color.White
                );

            spriteBatch.DrawString(Font1,
                "0,0",
                new Vector2(graphics.PreferredBackBufferWidth / 2, 30), 
                Color.White, 
                0,
                new Vector2(0, 0),
                3,
                SpriteEffects.None, 
                0);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
